package org.jugile.demo.domain;

import org.jugile.daims.Bo;
import org.jugile.daims.anno.*;
import org.jugile.util.Time;
import org.jugile.util.Money;
import org.jugile.demo.common.*;


@DaimsObject(table="book_t")
public class BookBase extends Bo {

	@Fld(size=300) // field
	private String title;
	public String getTitle() { return title; }
	public Book setTitle(String v) { return (Book)setFld("title",v); }
 
	@Fld // field
	private int pages;
	public int getPages() { return pages; }
	public Book setPages(int v) { return (Book)setFld("pages",v); }
 
	@Fld // field
	private BookType type;
	public BookType getType() { return type; }
	public Book setType(BookType v) { return (Book)setFld("type",v); }
 
	@Fld // field
	private Time published;
	public Time getPublished() { return published; }
	public Book setPublished(Time v) { return (Book)setFld("published",v); }
 
	@Fld // field
	private boolean free;
	public boolean getFree() { return free; }
	public Book setFree(boolean v) { return (Book)setFld("free",v); }
 
	@Fld(size=10000) // field
	private String desc;
	public String getDesc() { return desc; }
	public Book setDesc(String v) { return (Book)setFld("desc",v); }
 
	@Fld // field
	private double factor;
	public double getFactor() { return factor; }
	public Book setFactor(double v) { return (Book)setFld("factor",v); }
 
	@Fld // field
	private Money price;
	public Money getPrice() { return price; }
	public Book setPrice(Money v) { return (Book)setFld("price",v); }
 
 
	@ConnectionN1(c="children")
	private Book parent;
	public Book getParent() { return (Book)getN1("parent"); }
	public Book setParent(Book o) { return (Book)setN1("parent",o); }

	@Connection1N(o="parent")
	private BookCollection children = new BookCollection();
	public BookCollection getChildren() { return (BookCollection)getAll("children"); }
	public Book addChild(Book o) { add("children",o); return (Book)this; }
	public void removeChild(Book o) { remove("children",o); }

	public boolean isRoot() { return parent == null; }
	public Book getRoot() {
		Book p = getParent();
		if (p == null) return (Book)this;
		return p.getRoot();
	}

 
	// --- n - 1 ----

	@ConnectionN1(c="cMainBooks",name="mainAuthor_f")
	private Author authorMain;
	public Author getMainAuthor() { return (Author)getN1("authorMain"); }
	public Book setMainAuthor(Author o) { return (Book)setN1("authorMain",o); }


	@ConnectionN1(c="cCoBooks",name="coAuthor_f")
	private Author authorCo;
	public Author getCoAuthor() { return (Author)getN1("authorCo"); }
	public Book setCoAuthor(Author o) { return (Book)setN1("authorCo",o); }

 
	// --- 1 - n ----
 
	// --- n - n ---
 	@ConnectionNN(c="mBooks",first=true,table="map_library_2_book")
	private LibraryCollection mLibraries = new LibraryCollection();
	public LibraryCollection getLibraries() { return (LibraryCollection)getAll("mLibraries"); }
	public Book addLibrary(Library o) { add("mLibraries",o); return (Book)this; }
	public void removeLibrary(Library o) { remove("mLibraries",o); }
	
	public void toggle(Library v) {
		if (getLibraries().contains(v)) removeLibrary(v);
		else addLibrary(v);		
	}
	public boolean has(Library v) { return getLibraries().contains(v); }
	
 	@ConnectionNN(c="mSideBooks",first=true,table="map_library_2side_book")
	private LibraryCollection mSideLibraries = new LibraryCollection();
	public LibraryCollection getSideLibraries() { return (LibraryCollection)getAll("mSideLibraries"); }
	public Book addSideLibrary(Library o) { add("mSideLibraries",o); return (Book)this; }
	public void removeSideLibrary(Library o) { remove("mSideLibraries",o); }
	
	public void toggleSide(Library v) {
		if (getSideLibraries().contains(v)) removeLibrary(v);
		else addLibrary(v);		
	}
	public boolean hasSide(Library v) { return getSideLibraries().contains(v); }
	
 

	// ---- common utils ----
	protected Domain d() { return Domain.getDomain(); }


}